﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics.Contracts;

namespace Reggie.BLL.Entities
{
    /// <summary>
    /// POCO wrapping a regular expression and testing operations.
    /// </summary>
    public class RegExTest
    {
        /// <summary>
        /// Gets or sets the Regular Expression pattern.
        /// </summary>
        /// <value>The pattern.</value>
        public string Pattern { get; set;  }

        /// <summary>
        /// Gets or sets the input string to test.
        /// </summary>
        /// <value>The test string.</value>
        public string TestString { get; set; }

        /// <summary>
        /// Runs the test string through the regular expression pattern.
        /// </summary>
        /// <returns>string describing the match results</returns>
        public string TryPatternMatch()
        {
            //Contract.Requires<InvalidOperationException>(Pattern != null, "Pattern cannot be null");
            //Contract.Requires<InvalidOperationException>(TestString != null, "TestString cannot be null");

            Regex expression = new Regex(Pattern);
            MatchCollection matches = expression.Matches(TestString);

            if (matches.Count > 0)
            {
                StringBuilder output = new StringBuilder();
                int lineNumber = 1;
                foreach (Match m in matches)
                {
                    output.Append("[");
                    output.Append(lineNumber++);
                    output.Append("] ");
                    output.AppendLine(m.Value);
                }

                return output.ToString();
            }
            else
            {
                return "No matches found.";
            }
        }
    }
}
